import Phaser from "phaser";
import {ProgressBar} from "@/widgets/ProgressBar.ts";
import * as res from "@/sys/resource.ts";

export class Ocean1Preloader extends Phaser.Scene {

    private declare readonly mode: string;

    constructor(mode: string) {
        super("Ocean1Preloader");
        this.mode = mode;
    }

    preload(): void {
        const {width, height} = this.cameras.main;
        // 绘制背景
        this.add.rectangle(0, 0, width, height, 0x3AB2FE).setOrigin(0);
        // 绘制进度条
        const progressBar = new ProgressBar(this, width / 4, height / 4, width / 2, 30, 15,
            {
                bgColor: 0xffffff,
                strokeWidth: 1
            }
        );
        this.load.on('progress', (value: number) => {
            progressBar.setProgress(value);
        });
        this.load.image("BGReady", res.BGReady);
        this.load.image("SpriteReady1", res.SpriteReady1);
        this.load.image("SpriteReady2", res.SpriteReady2);
        this.load.image("SpriteReady3", res.SpriteReady3);
        this.load.image("SpriteReadyTip", res.SpriteReadyTips);
        this.load.image('BG1Tile', res.BG1Tile);
        this.load.spritesheet('SpritePlane', res.SpritePlane, {
            frameWidth: 145,
            frameHeight: 235,
        });

        this.load.atlas("SpritesTarget", res.SpritesTarget, res.JsonTargets)
        this.load.atlas("SpritesTargetRight", res.SpritesTargetRight, res.JsonTargets)
        this.load.atlas("SpritesTargetError", res.SpritesTargetError, res.JsonTargets)
        this.load.atlas("SpritesDisturb", res.SpritesDisturb, res.JsonTargets)
        this.load.atlas("SpritesDisturbError", res.SpritesDisturbError, res.JsonTargets)
        this.load.atlas("SpritesDisturbRight", res.SpritesDisturbRight, res.JsonTargets)
        this.load.atlas("SpritesFishRight", res.SpritesFishRight, res.JsonFishes)
        this.load.atlas("SpritesFishError", res.SpritesFishError, res.JsonFishes)
        this.load.atlas("SpritesNumber", res.SpritesNumber, res.JsonNumbers)
        this.load.atlas("SpritesNumberRight", res.SpritesNumberRight, res.JsonNumbers)
        this.load.atlas("SpritesNumberError", res.SpritesNumberError, res.JsonNumbers)

        this.load.audio("AudioEstimate", res.AudioEstimate);
        this.load.audio("AudioMultidimensional", res.AudioMultidimensional);
        this.load.audio("AudioCognition", res.AudioCognition);
        this.load.audio("AudioAmazing", res.AudioAmazing);
        this.load.audio("AudioError", res.AudioError);
        this.load.audio("AudioRight", res.AudioRight);
        this.load.audio("AudioFishRight", res.AudioFishRight);
        this.load.audio("AudioNumberRight", res.AudioNumberRight);

        this.load.image("ImgPause", res.ImgPause);
        this.load.image("ImgStart", res.ImgStart);

        this.load.image("TextError", res.TextError);
        this.load.image("TextRight", res.TextRight);
    }

    create(): void {
        const {width, height} = this.cameras.main;
        this.add.tileSprite(0, 0, width, height, 'BGReady')
            .setOrigin(0, 0)
            .setTilePosition(-120, 0)
            .setTileScale(width / 750, height / 1159);

        let ready1 = this.add.image(width / 2, height / 2, 'SpriteReady1')
            .setScale(0.5, 0.5);
        let ready2 = this.add.image(width / 2, height / 2, 'SpriteReady2')
            .setScale(0.5, 0.5);
        let ready3 = this.add.image(width / 2, height / 2, 'SpriteReady3')
            .setScale(0.5, 0.5);
        if (this.mode == '3') { // 评估模式才显示突破150分
            this.add.image(width / 2, height / 2 + 200, 'SpriteReadyTip');
        }
        let i: number = 0;
        this.time.addEvent({
            delay: 1000,
            repeat: 2,
            callback: () => {
                if (i == 0) {
                    ready3.destroy()
                } else if (i == 1) {
                    ready2.destroy()
                } else {
                    ready1.destroy();
                    this.scene.start("Ocean1Main");
                }
                i++
            }
        });
    }
}